package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.Drug;
import com.yixin.hospital.entity.domain.Guidance;
import com.yixin.hospital.entity.domain.VisitInspectionList;
import com.yixin.hospital.entity.domain.VisitInspectionRealReport;
import com.yixin.hospital.entity.dto.req.visitInspection.AddVisitInspectionReqDTO;
import com.yixin.hospital.entity.vo.VisitInspectionListVO;
import com.yixin.hospital.enums.DrugVisitStatusEnum;
import com.yixin.hospital.enums.GuidanceStatusEnum;
import com.yixin.hospital.service.DrugService;
import com.yixin.hospital.service.GuidanceService;
import com.yixin.hospital.service.VisitInspectionListService;
import com.yixin.hospital.service.VisitInspectionRealReportService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/visit-inspection")
@Api(value = "检查管理", tags = "检查管理")
public class VisitInspectionController {
    @Resource
    private VisitInspectionListService visitInspectionListService;
    @Resource
    private VisitInspectionRealReportService visitInspectionRealReportService;
    @Resource
    private DrugService drugService;
    @Resource
    private GuidanceService guidanceService;


    @PostMapping("/add-visit-inspection")
    @ApiOperation(value = "新增检查")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<?> addVisitInspection(@Valid @RequestBody AddVisitInspectionReqDTO addVisitInspectionReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        // 删除未缴费的检查
        visitInspectionListService.remove(new LambdaQueryWrapper<VisitInspectionList>().eq(VisitInspectionList::getGuidanceCode, addVisitInspectionReqDTO.getGuidanceCode())
                .eq(VisitInspectionList::getStatus, DrugVisitStatusEnum.PENDING.getCode()));
        List<VisitInspectionList> newList = addVisitInspectionReqDTO.getVisitInspectionList().stream().filter(visitInspectionList -> visitInspectionList.getStatus() == 0).collect(Collectors.toList());
        for (VisitInspectionList visitInspectionList : newList) {
            visitInspectionList.setGuidanceCode(addVisitInspectionReqDTO.getGuidanceCode());
            visitInspectionList.setCreatedBy(userName);
            visitInspectionList.setUpdatedBy(userName);
        }
        if (!CollectionUtils.isEmpty(addVisitInspectionReqDTO.getVisitInspectionList())) {
            List<VisitInspectionList> pendingList = addVisitInspectionReqDTO.getVisitInspectionList().stream().filter(visitInspectionList -> visitInspectionList.getStatus() == DrugVisitStatusEnum.PENDING.getCode()).collect(Collectors.toList());
            visitInspectionListService.saveBatch(pendingList);
        }
        // 重新存储计算金额
        visitInspectionRealReportService.remove(new LambdaQueryWrapper<VisitInspectionRealReport>()
                .eq(VisitInspectionRealReport::getGuidanceCode, addVisitInspectionReqDTO.getGuidanceCode()));
        for (VisitInspectionRealReport visitInspectionRealReport : addVisitInspectionReqDTO.getVisitInspectionRealReportList()) {
            visitInspectionRealReport.setGuidanceCode(addVisitInspectionReqDTO.getGuidanceCode());
            visitInspectionRealReport.setCreatedBy(userName);
            visitInspectionRealReport.setUpdatedBy(userName);
        }
        visitInspectionRealReportService.saveBatch(addVisitInspectionReqDTO.getVisitInspectionRealReportList());
        // 修改主记录状态
        Guidance guidance = guidanceService.getOne(new LambdaQueryWrapper<Guidance>().eq(Guidance::getGuidanceCode, addVisitInspectionReqDTO.getGuidanceCode()));
        guidance.setStatus(GuidanceStatusEnum.PAYING.getCode());
        guidance.setInspectionTime(LocalDateTime.now());
        guidanceService.updateById(guidance);
        return ApiResult.success();
    }


    @GetMapping("/get-visit-inspection-detail")
    @ApiOperation(value = "获取检查详情")
    public ApiResult<?> getVisitInspectionDetail(@RequestParam String guidanceCode) {
        List<VisitInspectionListVO> list = new ArrayList<>();
        List<VisitInspectionList> visitInspectionLists = visitInspectionListService.list(new LambdaQueryWrapper<VisitInspectionList>().eq(VisitInspectionList::getGuidanceCode, guidanceCode));
        for (VisitInspectionList visitInspectionList : visitInspectionLists) {
            Drug drug = drugService.getOne(new LambdaQueryWrapper<Drug>().eq(Drug::getCode, visitInspectionList.getCode()));
            VisitInspectionListVO visitInspectionListVO = new VisitInspectionListVO();
            BeanUtils.copyProperties(visitInspectionList, visitInspectionListVO);
            visitInspectionListVO.setName(drug.getName());
            visitInspectionListVO.setOuterUnit(drug.getOuterUnit());
            visitInspectionListVO.setCommonName(drug.getCommonName());
            list.add(visitInspectionListVO);
        }

        return ApiResult.success(list);
    }
}
